﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;

namespace Vincent.Register
{
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
        }

        private void FormMain_Load(object sender, EventArgs e)
        {
            SetUserInfo();
        }

        private void SetUserInfo()
        {
            button2.Enabled = false;
            pictureBox1.Visible = false;
            labLodding.Visible = true;
            // webBrowser1.Navigate("https://tw.edit.yahoo.com/registration?.intl=tw&.src=www&.done=http://tw.yahoo.com&hpp=");
            webBrowser1.Navigate("https://cn.edit.yahoo.com/registration?intl=cn&.done=http://cn.yahoo.com/&src=&last=&partner=yahoo_default");
            string data = "secondname=" + secondname.Text + "&firstname=" + this.firstname.Text + "&gender=" + gender.Text + "&yyyy=" + yyyy.Text +
                "&mm=" + mm.Text + "&dd=" + dd.Text + "&country=tw&postalcode=222&yahooid=" + yahooid.Text + "&password="
                + password.Text + "&secquestionanswer=" + secquestionanswer.Text +
                "&secquestionanswer2=" + secquestionanswer2.Text + "&captchaV5Answer=" + captchaV5Answer.Text;
            Random ra = new Random();
            secondname.Text = "四";
            firstname.Text = "张";
            yahooid.Text = "zhangsan_" + DateTime.Now.Day + DateTime.Now.Second + DateTime.Now.Minute + ra.Next();
        }
       

        HtmlElementCollection html = null;
        // 已注册列表
        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show(webBrowser1.ReadyState.ToString());
            string src = webBrowser1.Document.GetElementById("captchaV5ClassicCaptchaImg").GetAttribute("src");
            pictureBox1.ImageLocation = src;
            html = webBrowser1.Document.GetElementsByTagName("input");
        }

        // 手动注册
        private void button2_Click(object sender, EventArgs e)
        {

            string data = "secondname=" + secondname.Text + "&firstname=" + this.firstname.Text + "&gender=" + gender.Text + "&yyyy=" + yyyy.Text +
                "&mm=" + mm.Text + "&dd=" + dd.Text + "&country=tw&postalcode=222&yahooid=" + yahooid.Text + "&password="
                + password.Text + "&secquestionanswer=" + secquestionanswer.Text +
                "&secquestionanswer2=" + secquestionanswer2.Text + "&captchaV5Answer=" + captchaV5Answer.Text;

            if (html != null)
            {
                foreach (HtmlElement item in html)
                {
                    if (item.GetAttribute("type") == "hidden")
                    {
                        data += "&" + item.GetAttribute("id") + "=" + item.GetAttribute("value");
                    }
                }
            }

            PostWebRequest("https://tw.edit.yahoo.com/registration?.intl=tw&.src=www&.done=http://tw.yahoo.com&hpp=", data);


            webBrowser1.Document.GetElementById("secondname").SetAttribute("value", secondname.Text);
            webBrowser1.Document.GetElementById("firstname").SetAttribute("value", firstname.Text);
            webBrowser1.Document.GetElementById("gender").GetElementsByTagName("option")[1].SetAttribute("selected", "selected");
            webBrowser1.Document.GetElementById("yyyy").SetAttribute("value", yyyy.Text);
            webBrowser1.Document.GetElementById("mm").GetElementsByTagName("option")[1].SetAttribute("selected", "selected");
            webBrowser1.Document.GetElementById("dd").SetAttribute("value", dd.Text);
            webBrowser1.Document.GetElementById("country").SetAttribute("value", "tw");
            webBrowser1.Document.GetElementById("postalcode").SetAttribute("value", "222");
            webBrowser1.Document.GetElementById("yahooid").SetAttribute("value", yahooid.Text);
            webBrowser1.Document.GetElementById("password").SetAttribute("value", password.Text);
            webBrowser1.Document.GetElementById("passwordconfirm").SetAttribute("value", password.Text);


            webBrowser1.Document.GetElementById("secquestion").GetElementsByTagName("option")[1].SetAttribute("selected", "selected");
            webBrowser1.Document.GetElementById("secquestion2").GetElementsByTagName("option")[1].SetAttribute("selected", "selected");
            webBrowser1.Document.GetElementById("secquestionanswer").SetAttribute("value", secquestionanswer.Text);
            webBrowser1.Document.GetElementById("secquestionanswer2").SetAttribute("value", secquestionanswer2.Text);
            webBrowser1.Document.GetElementById("captchaV5Answer").SetAttribute("value", captchaV5Answer.Text);
            webBrowser1.ScriptErrorsSuppressed = true;
            webBrowser1.Document.GetElementById("IAgreeBtn").InvokeMember("Click");
        }



        private void SavaUserInfo()
        {
            if (webBrowser1.DocumentTitle.Contains("确认") || webBrowser1.DocumentTitle.Contains("確認"))
            {
                MessageBox.Show("注册成功..  用户名:" + "www." + yahooid.Text + "@" + label7.Text + " 密码:" + password.Text);


                //保存 数据
                //try
                //{
                //    string ff = System.Windows.Forms.Application.StartupPath + "\\UserInfo.txt";
                //    if (!File.Exists(ff))
                //    {
                //        File.Create(ff);
                //    }

                //    FileStream aFile = new FileStream(ff, FileMode.Open);
                //    StreamWriter sw = new StreamWriter(aFile);
                //    sw.WriteLine("用户名:" + "www." + yahooid.Text + "@" + label7.Text + " 密码:" + password.Text);
                //    sw.Close();
                //    aFile.Close();
                //}
                //catch (IOException ex)
                //{

                //    return;
                //}


                SetUserInfo();

            }

        }

        /// <summary>
        /// 发送注册信息
        /// </summary>
        /// <param name="postUrl"></param>
        /// <param name="paramData"></param>
        /// <returns></returns>
        private string PostWebRequest(string postUrl, string paramData)
        {
            string ret = string.Empty;
            try
            {
                byte[] byteArray = System.Text.Encoding.Default.GetBytes(paramData); //转化
                HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(postUrl));
                webReq.Method = "POST";
                webReq.ContentType = "application/x-www-form-urlencoded";

                webReq.ContentLength = byteArray.Length;
                Stream newStream = webReq.GetRequestStream();
                newStream.Write(byteArray, 0, byteArray.Length);//写入参数
                newStream.Close();
                HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();
                StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.Default);
                ret = sr.ReadToEnd();
                sr.Close();
                response.Close();
                newStream.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return ret;
        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {

            SavaUserInfo();
            timer1.Interval = 1000;
            timer1.Enabled = true;
            timer1.Start();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (webBrowser1.Document.GetElementById("domain") != null && webBrowser1.Document.GetElementById("domain").GetAttribute("value") != "")
            {
                label7.Text = "@" + webBrowser1.Document.GetElementById("domain").GetAttribute("value");
            }

            if (webBrowser1.Document.GetElementById("captchaV5ClassicCaptchaImg") != null)
            {
                if (webBrowser1.Document.GetElementById("captchaV5ClassicCaptchaImg").GetAttribute("src") != "")
                {
                    string src = webBrowser1.Document.GetElementById("captchaV5ClassicCaptchaImg").GetAttribute("src");
                    pictureBox1.ImageLocation = src;
                    pictureBox1.Visible = true;
                    labLodding.Visible = false;
                    button2.Enabled = true;
                    timer1.Enabled = false;
                    timer1.Stop();
                }
            }
        }

        /// <summary>
        /// 请求服务器
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        //private string CallService(string url)
        //{
        //    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
        //    HttpWebResponse res = req.GetResponse() as HttpWebResponse;
        //    Stream resStream = res.GetResponseStream();
        //    StreamReader sr = new StreamReader(resStream, System.Text.Encoding.UTF8);
        //    string result = sr.ReadToEnd();
        //    resStream.Close();
        //    resStream.Dispose();
        //    sr.Close();
        //    sr.Dispose();
        //    return result;
        //}


    }

}
